
#include "gui_input.h"

using namespace WtfGui;
using namespace WtfEngine;


/**** Dialog ****/

#define DIALOG_BUTTON(callback, btn, sCaption) \
	rButton = new Widgets::Button(callback->Bind(btn), sCaption); \
	mButtons->AddChild(rButton); \
	rButton->Move(iPos, 0); \
	rButton->ExpandToText(); \
	iPos += rButton->getSize()[0];
	
Dialog::Dialog(const String& sCaption, const String& sTitle, tButtonSet btns,
	   tDialogType type, const Window::tRef& rWndParent):
Window(sTitle), mrLblCaption(new Widgets::Label(sCaption)), mButtons(new WidgetContainer("")), mClickedButton(BTN_NONE)
{
	Widgets::Button::tRef rButton;
	int						iPos = 0;

	AddChild(mrLblCaption);
	Window::AddChild(mButtons);

	FunctorType<void, tButton>::tRef rCallback = new ObjFunctor<Dialog, void, tButton>(this, &Dialog::DialogButtonCallback);

	// Create buttons
	switch(btns) {
		case BTNS_OK:
			DIALOG_BUTTON(rCallback, BTN_OK, "Ok");
			break;
		case BTNS_OKCANCEL:
			DIALOG_BUTTON(rCallback, BTN_OK, "Ok");
			DIALOG_BUTTON(rCallback, BTN_CANCEL, "Cancel");
			break;
		case BTNS_YESNO:
			DIALOG_BUTTON(rCallback, BTN_YES, "Yes");
			DIALOG_BUTTON(rCallback, BTN_NO, "No");
			break;
		case BTNS_YESNOCANCEL:
			DIALOG_BUTTON(rCallback, BTN_YES, "Yes");
			DIALOG_BUTTON(rCallback, BTN_NO, "No");
			DIALOG_BUTTON(rCallback, BTN_CANCEL, "Cancel");
			break;
	};
};

void Dialog::DialogButtonCallback(WtfGui::Dialog::tButton btn) {
	mClickedButton = btn;

	// Request to close the dialog
	OnClose();
};

void Dialog::OnClose() {
	Window::OnClose();
	
	// Invoke callback routine
	if(!mrCallback.isNull())
		(*mrCallback)();
};

